﻿Imports System.Data.OracleClient
Imports MySql.Data.MySqlClient
Imports System.Data.SQLite
Imports System.Data.Common
Public Class LoginForm

  Private _connection As DbConnection
  Private _oracsbuilder As OracleConnectionStringBuilder
  Private _mysqlcsbuilder As MySqlConnectionStringBuilder
  Private _sqlitecsbuilder As SQLiteConnectionStringBuilder
  Private _config As Configuration = Configuration.GetObject

  Public ReadOnly Property Connection() As DbConnection
    Get
      Return _connection
    End Get
  End Property

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    End
  End Sub

  Private Function Login() As Boolean
    If _config.DatabaseType = ServiceManager.eConnectionType.Oracle Then
      _oracsbuilder = New OracleConnectionStringBuilder
      _oracsbuilder.DataSource = _config.OracleDataSource
      _oracsbuilder.UserID = UserIDTextBox.Text
      _oracsbuilder.Password = PasswortTextBox.Text
      _connection = New OracleConnection(_oracsbuilder.ConnectionString)
    ElseIf _config.DatabaseType = ServiceManager.eConnectionType.MySQL Then
      _mysqlcsbuilder = New MySqlConnectionStringBuilder
      _mysqlcsbuilder.Server = _config.MySQLServer
      _mysqlcsbuilder.Database = _config.MySQLDatabase
      _mysqlcsbuilder.UserID = UserIDTextBox.Text
      _mysqlcsbuilder.Password = PasswortTextBox.Text
      _connection = New MySqlConnection(_mysqlcsbuilder.ConnectionString)
    ElseIf _config.DatabaseType = ServiceManager.eConnectionType.SQLite Then
      _sqlitecsbuilder = New SQLiteConnectionStringBuilder
      _sqlitecsbuilder.DataSource = _config.SQLiteDataSource
      _connection = New SQLiteConnection(_sqlitecsbuilder.ConnectionString)
    End If

    Try
      _connection.Open()
    Catch oex As OracleException
      MsgBox(oex.Message, MsgBoxStyle.Critical, "Verbindungsfehler")
      Return False
    Catch ex As Exception
      MsgBox("Unbekannter Fehler bei der Verbindung:" & vbNewLine & ex.Message, MsgBoxStyle.Critical, "Verbindungsfehler")
      Return False
    End Try
    Return True
  End Function

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Login() = True Then
      Close()
    End If
  End Sub

  Private Sub LoginForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.PictureBox1.ImageLocation = Application.StartupPath & "\" & _config.StartupImageLeft
    Me.PictureBox2.ImageLocation = Application.StartupPath & "\" & _config.StartupImageRight
    If _config.DatabaseType = ServiceManager.eConnectionType.MySQL Then
      Me.DataSourceTextBox.Text = "MySQL"
    ElseIf _config.DatabaseType = ServiceManager.eConnectionType.Oracle Then
      Me.DataSourceTextBox.Text = "ORACLE"
    ElseIf _config.DatabaseType = ServiceManager.eConnectionType.SQLite Then
      Me.DataSourceTextBox.Text = "SQLite"
    End If
    Me.UserIDTextBox.Text = _config.DatabaseUserID
    Me.PasswortTextBox.Text = _config.DatabasePassword
    Refresh()
  End Sub

  Private Sub ConfigButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfigButton.Click
    Dim ief As New IniEditForm
    ief.ShowDialog()
    Me.UserIDTextBox.Text = _config.DatabaseUserID
    Me.PasswortTextBox.Text = _config.DatabasePassword
    If _config.DatabaseType = ServiceManager.eConnectionType.MySQL Then
      Me.DataSourceTextBox.Text = "MySQL"
    ElseIf _config.DatabaseType = ServiceManager.eConnectionType.Oracle Then
      Me.DataSourceTextBox.Text = "ORACLE"
    ElseIf _config.DatabaseType = ServiceManager.eConnectionType.SQLite Then
      Me.DataSourceTextBox.Text = "SQLite"
    End If
  End Sub
End Class